package ar.edu.itba.it.paw.web.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Transaction;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.model.TransactionPriceModel;

public class SoldProductsPage extends BasePage {

	@SpringBean
	UserRepo userRepo;
	
	public SoldProductsPage(){
		
		IModel<List<Transaction>> transactionModel = new LoadableDetachableModel<List<Transaction>>() {
			@Override
			protected List<Transaction> load() {
				
				return new ArrayList<Transaction>(((MercadoLibreSession.get().getUser()).getSold()));
			}
		};
		
		
		add(new PropertyListView<Transaction>("SoldTransactions", transactionModel) {
			
			@Override
			protected void populateItem(ListItem<Transaction> item) {
				item.add(new Label("buyer",new PropertyModel<Transaction>(item.getModel(), "buyer.username")).add(new SimpleAttributeModifier("style", "color: #FFFFFF")));
				item.add(new Label("quantity", new PropertyModel<Transaction>(item.getModel(), "quantity")).add(new SimpleAttributeModifier("style", "")));
				item.add(new Label("totalValue",new TransactionPriceModel(item.getModel()) ).add(new SimpleAttributeModifier("style","color: #FFFFFF")));
				item.add(new Label("date",new PropertyModel<Transaction>(item.getModel(), "date") ).add(new SimpleAttributeModifier("style","color: #FFFFFF")));
				item.add(new Label("product",new PropertyModel<Transaction>(item.getModel(), "product.name") ).add(new SimpleAttributeModifier("style","color: #FFFFFF")));
			}
		});
	}
}
